from pyspades.server import block_action
from pyspades.collision import distance_3d_vector
from commands import add, admin
from map import Map
from pyspades.constants import *
import commands

@admin
def wall(connection, value = None):
    value = int(value)
    if value < 63:
        connection.walling = value
        return 'building %s block high wall' % connection.walling
    else:
        return 'Wall is too high!'
add(wall)

def apply_script(protocol, connection, config):
    class WallMakerConnection(connection):
        walling = None
        def on_block_build(self, x, y, z):
            if self.walling is not None:
                for wall_z in xrange(z - int(self.walling) + 1, z):
                    if x > 0 and x < 512 and y > 0 and y < 512 and wall_z > 0 and wall_z < 63:
                        if self.god_build:
                            if self.protocol.god_blocks is None:
                                self.protocol.god_blocks = set()
                            self.protocol.god_blocks.add((x, y, wall_z))
                        block_action.x = x
                        block_action.y = y
                        block_action.z = wall_z
                        block_action.player_id = self.player_id
                        block_action.value = BUILD_BLOCK
                        self.protocol.send_contained(block_action, save = True)
                        self.protocol.map.set_point(x, y, wall_z, self.color+(255,), user = False)
            return connection.on_block_build(self, x, y, z)
    return protocol, WallMakerConnection